package com.naiterui.ehp.bs.patient.service.impl;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bp.domain.Patient;
import com.naiterui.ehp.bp.utils.date.DateUtil;
import com.naiterui.ehp.bs.patient.repository.IApPatientRepository;
import com.naiterui.ehp.bs.patient.service.ISuperviseService;
import com.naiterui.ehp.bs.patient.vo.SupervisePatientVO;

import cn.hutool.core.collection.CollectionUtil;

/**
 * <pre>
 *
 * </pre>
 *
 * @author fangguanhong
 * @date 2021/1/15 下午8:47
 * @since 1.0.0
 */
@Service
public class ApSuperviseServiceImpl implements ISuperviseService {

    @Autowired
    private IApPatientRepository patientRepository;

    @Override
    public List<SupervisePatientVO> getPatientInfo(String start, String end) {
        List<SupervisePatientVO> result = new ArrayList<>();
        Date startDate = null;
        Date endDate = null;
        if (StringUtils.isNotEmpty(start) && StringUtils.isNotEmpty(end)) {
            startDate = com.naiterui.ehp.bp.utils.date.DateUtil.parseStrToCustomPatternDate(start,
                    com.naiterui.ehp.bp.utils.date.DateUtil.DATE_TIME_FORMAT);
            endDate = com.naiterui.ehp.bp.utils.date.DateUtil.parseStrToCustomPatternDate(end,
                    com.naiterui.ehp.bp.utils.date.DateUtil.DATE_TIME_FORMAT);
        } else {
            String date = DateTimeFormatter.ofPattern("yyyy-MM-dd")
                                           .withZone(ZoneId.systemDefault()).format(Instant.now().minus(1, ChronoUnit.DAYS));
            startDate = com.naiterui.ehp.bp.utils.date.DateUtil.parseStrToCustomPatternDate(date + " 00:00:00",
                    com.naiterui.ehp.bp.utils.date.DateUtil.DATE_TIME_FORMAT);
            endDate = com.naiterui.ehp.bp.utils.date.DateUtil.parseStrToCustomPatternDate(date + " 23:59:59", DateUtil.DATE_TIME_FORMAT);
        }

        // 新增相关数据
        List<Patient> addPatients = patientRepository.findByChangedAtBetween(startDate, endDate);
        List<Long> addPatientIds = addPatients.stream().map(pt -> pt.getId()).collect(Collectors.toList());
        // 修改相关数据
        List<Patient> updatePatients = patientRepository.findByChangedAtBetween(startDate, endDate);
        // 真实的修改数据
        List<Patient> updateRealPatients = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(updatePatients)) {
            updateRealPatients = updatePatients.stream()
                                               .filter(patient -> !addPatientIds.contains(patient.getId()))
                                               .collect(Collectors.toList());
        }

        List<Patient> pushPatient = new ArrayList<>();
        pushPatient.addAll(addPatients);
        pushPatient.addAll(updatePatients);

        if (CollectionUtil.isNotEmpty(pushPatient)) {
            pushPatient.forEach(patient -> {
                SupervisePatientVO vo =
                        SupervisePatientVO.builder()
                                          .id(patient.getId())
                                          .idCard(patient.getIdCard())
                                          .name(patient.getName())
                                          .gender(patient.getGender())
                                          .maritalStatus(patient.getMaritalStatus())
                                          .birthday(DateUtil.parseDateToString(patient.getBirthday(), DateUtil.DATE_FORMAT))
                                          .age(patient.getAge())
                                          .national("汉族")
                                          .phone(patient.getPhone())
                                          .createdAt(DateUtil.parseDateToString(patient.getCreateAt(), DateUtil.DATE_TIME_FORMAT))
                                          .updateAt(DateUtil.parseDateToString(patient.getChangedAt(), DateUtil.DATE_TIME_FORMAT))
                                          .tag(addPatientIds.contains(patient.getId()) ? SupervisePatientVO.TAG_ADD : SupervisePatientVO.TAG_UPDATE)
                                          .build();
                result.add(vo);
            });
        }
        return result;
    }
}
